package com.mission.schedule.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mission.schedule.R;
import com.mission.schedule.bean.CYTXBean;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.my160920.picker.TimePicker1;
import com.mission.schedule.my160920.picker.TimePicker2;
import com.mission.schedule.utils.SharedPrefUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by liwei on 2017/1/16.
 */

public class QcActivity extends BaseActivity implements View.OnClickListener{

    LinearLayout top_ll_back;
    RelativeLayout top_ll_right;
    List<CYTXBean.QcBean> qcBeanList = new ArrayList<>();
    ListView listview;
    TXSetAdapter txSetAdapter;
    SharedPrefUtil sharedPrefUtil;
    String qcStr = "";
    String djsStr = "";

    @Override
    protected void setListener() {

    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_qcset);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        top_ll_back = (LinearLayout)findViewById(R.id.top_ll_back);
        top_ll_right = (RelativeLayout)findViewById(R.id.top_ll_right);
        listview = (ListView)findViewById(R.id.listview);
        top_ll_back.setOnClickListener(this);
        top_ll_right.setOnClickListener(this);

        sharedPrefUtil =  new SharedPrefUtil(this, ShareFile.USERFILE);
        cjson(sharedPrefUtil.getString(this,ShareFile.USERFILE,ShareFile.CYTXJSON,""));
    }

    @Override
    protected void setAdapter() {
        txSetAdapter = new TXSetAdapter(qcBeanList);
        listview.setAdapter(txSetAdapter);
        txSetAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.top_ll_back:
                finish();
                break;
            case R.id.top_ll_right:
                save();
                finish();
                break;
        }
    }

    private void save(){
        try {
            List<CYTXBean.QcBean> djsBeanList = txSetAdapter.getlist();
            JSONArray jsonArray = new JSONArray();
            for (int i = 0; i < djsBeanList.size(); i++) {
                JSONObject tmpObj = new JSONObject();
                tmpObj.put("time", djsBeanList.get(i).getTime());
                tmpObj.put("timetype", djsBeanList.get(i).timetype);
                tmpObj.put("type", djsBeanList.get(i).type);
                jsonArray.put(tmpObj);
            }
            qcStr = "\"qc\":" + jsonArray.toString();
            String json = "{" + qcStr +","+ djsStr + "}";
            sharedPrefUtil.putString(QcActivity.this, ShareFile.USERFILE,
                    ShareFile.CYTXJSON, json);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public class TXSetAdapter extends BaseAdapter {

        List<CYTXBean.QcBean> qcBeanList;
        public TXSetAdapter(List<CYTXBean.QcBean> qcBeanList){
            this.qcBeanList = qcBeanList;
        }


        @Override
        public int getCount() {
            return qcBeanList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null){
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(QcActivity.this).inflate(R.layout.txset_item,null);
                viewHolder.timecontent = (TextView)convertView.findViewById(R.id.timecontent);
                viewHolder.timetype = (TextView)convertView.findViewById(R.id.timetype);
                viewHolder.rl = (RelativeLayout) convertView.findViewById(R.id.rl);
                convertView.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.timecontent.setText(qcBeanList.get(position).getTime()+"");
            viewHolder.timetype.setText(qcBeanList.get(position).getTimetype()+"");
            viewHolder.rl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    addPersonCalender(position);
                }
            });
            return convertView;
        }

        class ViewHolder{
            TextView timetype,timecontent;
            RelativeLayout rl;
        }

        public List<CYTXBean.QcBean> getlist() {
            return qcBeanList;
        }

        private void addPersonCalender(int position) {
            Dialog dialog = new Dialog(QcActivity.this, R.style.dialog_translucent);
            Window window = dialog.getWindow();
            android.view.WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
            params.alpha = 0.92f;
            window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
            window.setAttributes(params);// 设置生效

            LayoutInflater fac = LayoutInflater.from(QcActivity.this);
            View more_pop_menu = fac.inflate(R.layout.tx_time_dialog, null);
            dialog.setCanceledOnTouchOutside(true);
            dialog.setContentView(more_pop_menu);
            params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            params.width = ViewGroup.LayoutParams.MATCH_PARENT;

            new alterAddDialogOnClick(dialog, more_pop_menu, position);
        }

        /**
         * 添加用户日程dialog,点击事件
         */
        class alterAddDialogOnClick {

            private View view;
            private Dialog dialog;
            TimePicker2 tp_test;
            int position;

            public alterAddDialogOnClick(Dialog dialog, View view, int position) {
                this.dialog = dialog;
                this.view = view;
                this.position = position;
                initview();
                //滚动组布局
                dialog.show();
            }


            private void initview() {
                tp_test = (TimePicker2) view.findViewById(R.id.tp_test1);
                tp_test.setVisibility(View.VISIBLE);

                String[] t = qcBeanList.get(position).getTime().split(":");
                tp_test.setcuitem1(Integer.valueOf(t[0]) - 1);
                tp_test.setcuitem2(Integer.valueOf(t[1]) - 1);

                tp_test.setOnChangeListener(tp_onchanghelistener);
            }

            TimePicker2.OnChangeListener tp_onchanghelistener = new TimePicker2.OnChangeListener() {
                @Override
                public void onChange(String hour, String minute) {
                    qcBeanList.get(position).setTime(hour+":"+minute);
                    if (Integer.valueOf(hour) >= 0 && Integer.valueOf(hour) < 5 ){
                        qcBeanList.get(position).setTimetype("凌晨");
                        qcBeanList.get(position).setType(1);
                    } else if (Integer.valueOf(hour) >= 5 && Integer.valueOf(hour) < 9 ){
                        qcBeanList.get(position).setTimetype("早上");
                        qcBeanList.get(position).setType(2);
                    } else if (Integer.valueOf(hour) >= 9 && Integer.valueOf(hour) < 12 ){
                        qcBeanList.get(position).setTimetype("上午");
                        qcBeanList.get(position).setType(3);
                    } else if (Integer.valueOf(hour) >= 12 && Integer.valueOf(hour) < 18 ){
                        qcBeanList.get(position).setTimetype("下午");
                        qcBeanList.get(position).setType(4);
                    } else if (Integer.valueOf(hour) >= 18 && Integer.valueOf(hour) <= 24 ){
                        qcBeanList.get(position).setTimetype("晚上");
                        qcBeanList.get(position).setType(5);
                    }
                    notifyDataSetChanged();
                }
            };
        }
    }

    private void cjson(String json){
        try {
            if (json.length() > 0) {
                qcBeanList.clear();
                JSONObject jsonObject = new JSONObject(json);
                JSONArray qc = jsonObject.getJSONArray("qc");
                djsStr = "\"djs\":" + jsonObject.getJSONArray("djs").toString();
                for (int i = 0; i < qc.length(); i++) {
                    JSONObject jsonObject1 = qc.getJSONObject(i);
                    CYTXBean.QcBean qcBean = new CYTXBean.QcBean();
                    qcBean.setTime(jsonObject1.getString("time"));
                    qcBean.setTimetype(jsonObject1.getString("timetype"));
                    int fallback = 1;
                    if (jsonObject1.getString("timetype").equals("凌晨")){
                        fallback = 1;
                    } else if (jsonObject1.getString("timetype").equals("早上")){
                        fallback= 2;
                    } else if (jsonObject1.getString("timetype").equals("上午")){
                        fallback = 3;
                    } else if (jsonObject1.getString("timetype").equals("下午")){
                        fallback = 4;
                    } else if (jsonObject1.getString("timetype").equals("晚上")){
                        fallback = 5;
                    }
                    qcBean.setType(jsonObject1.optInt("type",fallback));
                    qcBeanList.add(qcBean);
                }
                Comparator<CYTXBean.QcBean> comparator = new Comparator<CYTXBean.QcBean>() {
                    @Override
                    public int compare(CYTXBean.QcBean lhs, CYTXBean.QcBean rhs) {
                        if (lhs.getType() == rhs.getType()) {
                            return lhs.getTime().compareTo(rhs.getTime());
                        } else {
                            return lhs.getType() - rhs.getType();
                        }
                    }
                };
                Collections.sort(qcBeanList,comparator);
            }
            setAdapter();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}